﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using University.Workshop.Presenters.AppointmentFolder;
using University.Workshop.Model;

namespace University.Workshop.WinApp.AppointmentFolder
{
    public partial class RegisterAppointmentView : Form, IRegisterAppointmentView
    {
        private RegisterAppointmentPresenter presenter;
        private Model.User userLogged;

        public RegisterAppointmentView()
        {
            InitializeComponent();
            presenter = new RegisterAppointmentPresenter(this);
        }

        public RegisterAppointmentView(Model.User user)
        {
            InitializeComponent();
            presenter = new RegisterAppointmentPresenter(this);
            userLogged = user;
        }

        public RegisterAppointmentView(string date, string time)
        {
            InitializeComponent();
            presenter = new RegisterAppointmentPresenter(this);
            string[] splittedDate = date.Split('/');
            txBxDay.Text = splittedDate[1];
            txBxMonth.Text = splittedDate[0];
            txBxYear.Text = splittedDate[2];
            cmBxTime.Text = time;

            cmBxTime.Enabled= false;
            txBxDay.Enabled=false;
            txBxMonth.Enabled = false;
            txBxYear.Enabled = false;
        }

        private void btnAccept_Click(object sender, EventArgs e)
        {
            bool verify = presenter.RegisterAppointment();
            if(verify)
                this.Close();
        }

        #region Implementation

        public Appointment NewAppointment
        {
            set
            {
                txBxPatientId.Text = value.PatientId;
                cmBxSpecialty.Text = value.Specialty;
                cmdDoctor.Text = value.DoctorId;
                cmBxTime.Text = value.Time;
                txBxDescription.Text = value.Description;
                txBxObservations.Text = value.Observations;
            }
            get 
            {
                return new Appointment(txBxPatientId.Text,
                                       cmBxSpecialty.Text,
                                       cmdDoctor.Text,
                                       txBxMonth.Text+"/"+txBxDay.Text+"/"+txBxYear.Text,
                                       cmBxTime.Text,
                                       txBxDescription.Text,
                                       txBxObservations.Text);
            }
        }

        public void SetMessage(string message)
        {
            MessageBox.Show(message);
        }
        
        #endregion

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnClean_Click(object sender, EventArgs e)
        {
            presenter.CleanInputFields();
        }

        private void txBxPatientId_KeyPress(object sender, KeyPressEventArgs e)
        {

        }

        private void txBxDoctorId_KeyPress(object sender, KeyPressEventArgs e)
        {

        }

        private void txBxDay_KeyPress(object sender, KeyPressEventArgs e)
        {

        }

        private void txBxMonth_KeyPress(object sender, KeyPressEventArgs e)
        {

        }

        private void txBxYear_KeyPress(object sender, KeyPressEventArgs e)
        {

        }

        private void btnNewPatient_Click(object sender, EventArgs e)
        {
            PatientRegister patient = new PatientRegister();
            patient.Show();
        }

        private void cmBxSpecialty_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void RegisterAppointmentView_Load(object sender, EventArgs e)
        {
            List<User> doctors = (List<User>) DataAccess.UserDataAccess.GetAllUsersIdsByRole("Doctor");
            List<string> doctorsIds = new List<string>();
            foreach(var doctor in doctors)
            {
                doctorsIds.Add(doctor.UserId);
            }
            cmdDoctor.DataSource = doctorsIds;
        }

    }
}
